import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import {request} from './request'
import Element from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css';
Vue.prototype.myCode = function(){
    return request;
}
Vue.prototype.HandString = function (){
    //获取字节长度(百度方法)
  this.GetLength = function (str) {
        return str.replace(/[\u0391-\uFFE5]/g,"aa").length;  //先把中文替换成两个字节的英文，在计算长度
    }
  this.getStr =  function (str,fontSize,count){
        var strTe=''
        var len = 0;
        for (let i = 0; i < str.length; i++) { // 遍历字符串
            if (/[\u4e00-\u9fa5]/.test(str[i])) { // 中文 长度为两字节
                len += 2;
            } else {
                len += 1;
            }
            if (len > fontSize-count) {
                break;
            } else {
                strTe += str[i];
            }
        }
        str = strTe
        return str;
    }

this.HandelContent =  function (content,size,more){

        content = content.replace(/(<\/?a.*?>)|(<\/?span.*?>)/g, '');   //去除a标签保留原有内容
        content = content.replace( /<img[^>]*>/,'')  //去除img标签

        var arr = content.match(/<p>.*?<\/p>/g);
        if(arr == null){
            return ' ';
        }
        var num = 0;
        var text='';
        var size = size
        var jude = false
        for(var i=0;i<arr.length;i++){
            if(i>=2){
                jude = true
                break;
            }
            var str = arr[i].replace('<p>','');
            str = str.replace('</p>','');
            var fontSize = this.GetLength(str);
            num += fontSize;
            if(num > size){

                var count =  num - size;
                var str = this.getStr(str,fontSize,count)
                text += '<p>'+str+'......</p>'
                jude = true;
                break;
            }
                text += '<p>' + str + '</p>'
        }
        if (jude && more ) {
            text += '<span >查看更多</span>'
        }
        return text;
    }

}
Vue.use(Element);
Vue.config.productionTip = false

router.beforeEach((to,from,next)=>{
    if(localStorage.getItem('API')){
        store.commit('isLoginChange',true)
    }
    store.commit('UpdateScroll',0);
   if(to.path.indexOf('BlogDetails')!= -1){
          store.commit('SetDisplay',true);
   }else{
          store.commit('SetDisplay',false);
   }
   store.commit('SetTitleData',0);
   store.commit('SetCountId','');
    next();
})
new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')



